﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.ExceptionHandling;
using System.Collections;
using CSharp.Libs.Net;
using CSharp.Libs.Misc;

namespace CSharp.Libs.WPF.UserControls
{
    class AutoKocUsersModel
    {
        #region declarations
        private ExceptionCollector _EC = null;

        #region constants
        private const String URL_MAIN = "http://bommel.dyndns-web.com/~Bommel/autokoc/allowedusers.php?";
        private const String URL_PARAM_ACTION = "action=";
        private const String URL_PARAM_FBUID = "FbUid=";
        private const String URL_PARAM_NAME = "name=";
        private const String URL_ACTION_VALUE_GETTABLE = "gettable";
        private const String URL_ACTION_VALUE_INSERT = "insert";
        private const String URL_ACTION_VALUE_DELETE = "delete";
        #endregion
        #region events
        //public delegate void ReceivingEventsChangedEventHandler(object sender);
        //public event ReceivingEventsChangedEventHandler ReceivingEventsChanged;
        //private void ThrowReceivingEventsChangedEvent()
        //{
        //    if (ReceivingEventsChanged != null)
        //    {
        //        ReceivingEventsChanged(this);
        //    }
        //}
        #endregion
        #endregion



        #region constructor / terminator
        public AutoKocUsersModel(
            ExceptionCollector EC)
        {
            _EC = EC;
        }

        ~AutoKocUsersModel()
        {
        }
        #endregion



        #region private functions
        /// <summary>_GetTable</summary>
        /// <returns></returns>
        /// <history>28.07.2011</history>
        private ArrayList _GetTable()
        {
            try
            {
                //build command
                String strGetTableURL = String.Concat(
                    URL_MAIN,
                    URL_PARAM_ACTION,
                    URL_ACTION_VALUE_GETTABLE);

                //post command
                PostSubmitter PS = new PostSubmitter(strGetTableURL);
                String strResult = PS.Post();
                PS = null;

                //prepare result
                Hashtable htResult = JSON.JsonDecode(strResult) as Hashtable;
                if (htResult != null)
                {
                    if (htResult["Data"] != null)
                    {
                        ArrayList aryData = htResult["Data"] as ArrayList;
                        return aryData;
                    }
                }
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }

            return null;
        }

        /// <summary>_InsertFbUid</summary>
        /// <param name="FbUid"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        /// <history>28.07.2011</history>
        private String _InsertFbUid(
            Int64 FbUid,
            String Name)
        {
            try
            {
                //build command
                String strGetTableURL = String.Concat(
                    URL_MAIN,
                    URL_PARAM_ACTION,
                    URL_ACTION_VALUE_INSERT,
                    "&", URL_PARAM_FBUID, FbUid.ToString(),
                    "&", URL_PARAM_NAME, Name);

                //post command
                PostSubmitter PS = new PostSubmitter(strGetTableURL);
                String strResult = PS.Post();
                PS = null;

                return strResult;
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }

            return "";
        }

        

        /// <summary>_DeleteFbUid</summary>
        /// <param name="FbUid"></param>
        /// <returns></returns>
        /// <history>28.07.2011</history>
        private String _DeleteFbUid(
            Int64 FbUid)
        {
            try
            {
                //build command
                String strGetTableURL = String.Concat(
                    URL_MAIN,
                    URL_PARAM_ACTION,
                    URL_ACTION_VALUE_DELETE,
                    "&", URL_PARAM_FBUID, FbUid.ToString());

                //post command
                PostSubmitter PS = new PostSubmitter(strGetTableURL);
                String strResult = PS.Post();
                PS = null;

                return strResult;
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }

            return "";
        }
        #endregion



        #region interface
        #region properties
        #endregion

        #region methods
        public ArrayList GetTable()
        {
            return _GetTable();
        }

        public String InsertFbUid(
            Int64 FbUid,
            String Name)
        {
            return _InsertFbUid(FbUid, Name);
        }

        public String DeleteFbUid(
            Int64 FbUid)
        {
            return _DeleteFbUid(FbUid);
        }
        #endregion
        #endregion
    }
}
